//////////////////////////////////////////////////////////////////////////////////
//                                                                              //
//  This file is part of the buola project (https://code.google.com/p/buola/).  //
//                                                                              //
//  Copyright(c) 2007-2012 Xavi Gratal                                          //
//  gratal AT gmail DOT com                                                     //
//                                                                              //
//  Buola is free software: you can redistribute it and/or modify               //
//  it under the terms of the GNU General Public License as published by        //
//  the Free Software Foundation, either version 3 of the License, or           //
//  (at your option) any later version.                                         //
//                                                                              //
//  Buola is distributed in the hope that it will be useful,                    //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of              //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               //
//  GNU General Public License for more details.                                //
//                                                                              //
//  You should have received a copy of the GNU General Public License           //
//  along with buola.  If not, see <http://www.gnu.org/licenses/>.              //
//                                                                              //
//////////////////////////////////////////////////////////////////////////////////

#include <buola/tty/input.h>
#include <buola/tty/clinereader.h>
#include <buola/io/curi.h>
#include <buola/python/types.h>
#include <buola/app/ccmdline.h>
#include "cinterpreter.h"
#include <fcntl.h>
#include <csignal>

using namespace buola;

buola::CCmdLineOption<std::string> sCommandOption('c',L"execute this command");

int main(int pArgC,char **pArgs)
{
    buola_init(pArgC,pArgs);    
    std::signal(SIGINT,SIG_IGN);
    std::signal(SIGTSTP,SIG_IGN);
    
    bush::gInterpreter=new bush::CInterpreter;
    
    if(cmd_line().IsSet(sCommandOption))
    {
        bush::gInterpreter->EvalString(utf32(cmd_line().GetValue(sCommandOption)),
                                       python::get_main_module().GetDict(),python::get_main_module().GetDict());
        return buola_finish();
    }
    
    tty::CLineReader lReader;
    lReader.sCompleteW.Connect(&bush::CInterpreter::Complete,bush::gInterpreter,_1,_2,_3);
    
    lReader.LoadHistory(io::get_home()/".bush_history");
    
    bush::gInterpreter->SetPrompt(L"%{yellow}%u@%h%{normal}:%{blue}%W%{normal}> ");
    bush::gInterpreter->SourceFile(io::get_home()/".bushrc",python::get_main_module().GetDict(),
                                                 python::get_main_module().GetDict());
    std::wstring lAccum;
    while(!lReader.IsEOF())
    {
        try
        {
            tty::set_window_title(bush::gInterpreter->GetWindowTitle());
            lReader.SetPrompt(bush::gInterpreter->GetPrompt());
            lReader.SetIndentPositions(bush::gInterpreter->GetIndentLevels());
            std::wstring lLine=lReader.ReadWLine();

            bush::gInterpreter->FeedLine(lLine);

            if(!lLine.empty())
                lReader.AddToHistory(lLine);
        }
        catch(XInternal &pE)
        {
            bush::gInterpreter->CancelLine();
        }
        
        bush::gInterpreter->CheckJobs();
    }
    
    lReader.SaveHistory(io::get_home()/".bush_history");

    return buola_finish();
}
